package com.topscomm.mis.cbb.pm.service.abs;

import com.topscomm.basic.BasicEntity;
import com.topscomm.basic.exception.MessageException;
import com.topscomm.basic.exception.ServiceException;
import com.topscomm.cbo.pojo.CboRoleEntity;
import com.topscomm.cbo.pojo.CboUserRoleEntity;
import com.topscomm.cbo.service.ICboItemDetailsService;
import com.topscomm.cbo.service.ICboRoleService;
import com.topscomm.cbo.service.ICboUserRoleService;
import com.topscomm.mis.cbb.pm.api.ITaskProgressService;
import com.topscomm.mis.cbb.pm.api.ITaskService;
import com.topscomm.mis.cbb.pm.base.pojo.CbbPmTaskEntity;
import com.topscomm.mis.cbb.pm.base.pojo.CbbPmTaskProgressEntity;
import com.topscomm.mis.cbb.pm.base.service.ICbbPmTaskBackRecordService;
import com.topscomm.mis.cbb.pm.base.service.ICbbPmTaskEditDocService;
import com.topscomm.mis.cbb.pm.base.service.ICbbPmTaskProgressService;
import com.topscomm.mis.cbb.pm.base.service.ICbbPmTaskService;
import com.topscomm.mis.cbb.pm.util.FlowNode;
import com.topscomm.mis.cbb.pm.util.PmSystemConst;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.SpringUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.vo.Page;
import com.topscomm.pub.vo.Query;
import com.topscomm.tap.common.DatacodeCache;
import org.apache.xmlbeans.SchemaBookmark;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@ConditionalOnMissingBean(ITaskProgressService.class)
public abstract class AbstractTaskProgressService implements ITaskProgressService {
    @Autowired
    private ICbbPmTaskService cbbPmTaskService;
    @Autowired
    private ICbbPmTaskProgressService cbbPmTaskProgressService;
    @Autowired
    private ICbbPmTaskEditDocService cbbPmTaskEditDocService;
    @Autowired
    private ICbbPmTaskBackRecordService cbbPmTaskBackRecordService;
    @Autowired
    private ICboRoleService cboRoleService;


    @Override
    public Map<String, Object> insert(Map<String, Object> paramMap) {
        beforeInsert(paramMap);
        CbbPmTaskProgressEntity cbbPmTaskProgressEntity = new CbbPmTaskProgressEntity();
        cbbPmTaskProgressEntity.convertFromMap(paramMap);
        cbbPmTaskProgressEntity = cbbPmTaskProgressService.insert(cbbPmTaskProgressEntity);
        Map<String, Object> dataMap = cbbPmTaskProgressEntity.convertToMap();
        afterInsert(dataMap);
        return dataMap;
    }

    protected void beforeInsert(Map<String, Object> paramMap) throws ServiceException {
    }

    protected void afterInsert(Map<String, Object> paramMap) throws ServiceException {
    }


    @Override
    public void update(Map<String, Object> paraMap) {
        cbbPmTaskProgressService.update(paraMap);
    }

    @Override
    public void delete(Map<String, Object> paraMap) {
        String ids = ConvertUtil.convertToString(paraMap.get("ids"));
        cbbPmTaskProgressService.deleteByWhere(" id in (" + ids + ")");
    }

    @Override
    public Map<String, Object> load(Map<String, Object> paraMap) {
        Map<String, Object> taskInfo = cbbPmTaskProgressService.queryMapById(paraMap.get("id").toString());
        this.loaded(taskInfo);
        return taskInfo;
    }

    protected void loaded(Map<String, Object> taskInfo) throws ServiceException {

    }

    @Override
    public Page list(Map<String, Object> paraMap, String sqlWhere) {
        Query query = new Query(paraMap);
        int totalCount = cbbPmTaskProgressService.getCount(sqlWhere);
        List<Map<String, Object>> tempList = null;
        if (query.getPageSize() > 0) {
            tempList = cbbPmTaskProgressService.queryMapForPage(sqlWhere, query.getCurrentPage(), query.getPageSize(),
                    query.getSidx(), query.getSord());
        } else {
            tempList = cbbPmTaskProgressService.queryMapByWhere(sqlWhere);
        }
        return new Page(tempList, totalCount, query.getPageSize(), query.getCurrentPage());
    }


    /**
     * @Description:    我的任务特殊查询方法、勿动
     * @Author:         wuyuyang
     * @Return:         com.topscomm.pub.vo.Page
     * @exception:
     * @Date:           14/7/2023 上午10:52
     * @Version:        1.0
     **/
    public Page listforMytask(Map<String, Object> paraMap, String sqlWhere) {
        Query query = new Query(paraMap);
        paraMap.put("whereSql", sqlWhere);
        int totalCount = cbbPmTaskService.getCount(paraMap,"getCountforMytask");
        List<Map<String, Object>> tempList = null;
        if (query.getPageSize() > 0) {
            tempList = cbbPmTaskService.queryMapForPage(sqlWhere, query.getCurrentPage(), query.getPageSize(),
                    query.getSidx(), query.getSord(),"queryMapForPageforMytask");
        } else {
            tempList = cbbPmTaskService.queryMapByWhere(sqlWhere,"queryMapByWhereforMytask");
        }
        cbbPmTaskService.setDispFields(tempList);
        return new Page(tempList, totalCount, query.getPageSize(), query.getCurrentPage());
    }

    /**
     * @Description:    判断对应人员是否为wbs管理人员
     * @Author:         wuyuyang
     * @Return:         boolean
     * @exception:
     * @Date:           28/7/2023 下午2:47
     * @Version:        1.0
     **/
    public boolean checkSysCbbAdmin(Map<String, Object> paraMap) {
        String type = ConvertUtil.convertToString(paraMap.get("type"));
        String usercode = ConvertUtil.convertToString(paraMap.get("usercode"));
        if (type == null || usercode == null) {
            throw new MessageException("获取单据类型或用户工号失败，请联系管理员！");
        }
        // 查询WbsPublish中系统对应管理角色
        DatacodeCache datacodeCache = (DatacodeCache) SpringUtil.getBean("datacodeCache");
        String userRole = datacodeCache.getValueDynamic("WbsPublish", type, "code", "name");
        if(userRole.isEmpty()) {
            throw new MessageException("获取"+ type +"wbs管理角色失败，请联系管理员！");
        }
        StringBufferProxy sql = new StringBufferProxy();
        sql.appendSingle("code = '{0}'", userRole);
        CboRoleEntity cboRoleEntity = cboRoleService.queryObjectByWhere(sql.toString());
        if(cboRoleEntity == null) {
            throw new MessageException("查询"+ userRole +"角色失败，请联系管理员！");
        }
        // 判断当前人员角色
        sql.clear();
        sql.appendSingle("b.code = '{0}' and a.roleid = '{1}'", usercode, cboRoleEntity.getId());
        List<Map<String, Object>> list = cbbPmTaskProgressService.queryMapByWhere(sql.toString(), "checkSysCbbAdmin");
        if(list.size() == 0){
            return false;
        }
        return true;
    }

}
